package com.cy.personer.web;

import com.cy.personer.bean.Role;
import com.cy.personer.biz.PowerBiz;
import com.cy.personer.biz.RoleBiz;
import com.cy.personer.biz.impl.PowerBizImpl;
import com.cy.personer.biz.impl.RoleBizImpl;
import com.cy.personer.mapper.RoleMapper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleHandler {

    private Role role = new Role();
    private Integer roleId;
    private String roleName;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleBizImpl roleBizImpl;

    @RequestMapping(value = "/role.action")
    public String query(HttpSession session, Integer pageNum) {
        // HttpSession session =request.getSession();
        if (pageNum == null||pageNum==0) {
                pageNum = 1;
        }
            PageInfo<Role> roleList = roleBizImpl.getRoles(pageNum);
            session.setAttribute("roleList", roleList);
            return "admin/role";
//       if(roleList!=null){
//           session.setAttribute("roleList",roleList);
//           return "admin/role";
//       }else {
//           return "admin/main";
//       }
        }



 //增加页面
@RequestMapping(value=("add.action"),method= RequestMethod.POST, produces="application/json;charset=utf-8")
  public @ResponseBody Boolean add(Role role){
     boolean addrole =roleMapper.add(role);
     if(addrole){
         return true;
     }else {
         return false;
     }
  }


    /*修改*/
    @RequestMapping(value=("update.action"),method= RequestMethod.POST, produces="application/json;charset=utf-8")
    public @ResponseBody Boolean update(Role role){
//        role.setRoleId(roleId);
//        role.setRoleName(roleName);
       boolean up=roleMapper.update(role);
       if (up){
           return true;
       }else{
           return false;
       }
    }
    /*删除*/
    @RequestMapping(value=("del.action"),method= RequestMethod.POST, produces="application/json;charset=utf-8")
    public @ResponseBody
    Boolean del(@RequestParam int roleId){
        boolean uprole=roleBizImpl.del(roleId);
        if (uprole){
            return true;
        }else{
            return false;
        }
    }


    public RoleBizImpl getRoleBizImpl() {
        return roleBizImpl;
    }

    public void setRoleBizImpl(RoleBizImpl roleBizImpl) {
        this.roleBizImpl = roleBizImpl;
    }

    public RoleMapper getRoleMapper() {
        return roleMapper;
    }

    public void setRoleMapper(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public Integer getRoleId() {
        return roleId;
    }

    public void setRoleId(Integer roleId) {
        this.roleId = roleId;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }
}


